﻿Imports System.Data
Partial Class WebForm_ManageRole_OpenManageBrandMapping
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

        End If
    End Sub

    Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim RowIndex As Integer = DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex
        Dim Division As String = Me.gv.DataKeys(RowIndex).Item("Division").ToString()
        Dim Brand As String = Me.gv.DataKeys(RowIndex).Item("Brand").ToString()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("delete from RoleBrandMapping ")
        sql.AppendFormat("where RoleID='{0}' ", Me.RoleID)
        sql.AppendFormat("and Division='{0}' ", Division)
        sql.AppendFormat("and Brand='{0}' ", Brand.Replace("'", "''"))
        If DirectCast(sender, CheckBox).Checked = True Then
            sql.AppendFormat(";insert into RoleBrandMapping(RoleID,Division,Brand) ")
            sql.AppendFormat("Select ")
            sql.AppendFormat("'{0}' ", Me.RoleID)
            sql.AppendFormat(",'{0}' ", Division)
            sql.AppendFormat(",'{0}' ", Brand.Replace("'", "''"))
        End If

        dat.AccData(sql.ToString())
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.InitDataAndShow()
    End Sub

    Protected Sub gv_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.PageIndexChanged
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

        End If
    End Sub

    Public Sub InitDataAndShow()
        Me.mpSearch.Show()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select distinct a.* ")
        sql.AppendFormat(",Case isnull(b.RoleID,'') when '' then '0' else '1' end CanAccess ")
        sql.AppendFormat(",c.Description as SignatureText ")
        sql.AppendFormat("from DivisionBrandMappingView a ")
        sql.AppendFormat("left join RoleBrandMapping b ")
        sql.AppendFormat("on a.Division=b.Division and a.Brand=b.Brand ")
        sql.AppendFormat("and b.RoleID ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat(")")
        sql.AppendFormat("left join ProductHierarchyOriginal c ")
        sql.AppendFormat("on a.Brand=c.Code ")
        sql.AppendFormat("where a.Division ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select Division from Role where RoleID ")
        sql.AppendFormat("	in ")
        sql.AppendFormat("	( ")
        sql.AppendFormat("		select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat("	) ")
        sql.AppendFormat(") ")
        sql.AppendFormat("and c.Code is not null")

        'MyObj.WriteLog(sql.ToString())
        Me.gv.DataSource = dat.GetData(sql.ToString())
        Me.gv.DataBind()

    End Sub

    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "1", True, False))
    End Function

    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property
End Class
